import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/layout',
    name: 'Layout',
    component: () => import('../views/Layout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '首页', requiresAuth: true }
      },
      {
        path: '/schedule',
        name: 'Schedule',
        component: () => import('../views/schedule/Schedule.vue'),
        meta: { title: '课程表', requiresAuth: true }
      },
      {
        path: '/course-selection',
        name: 'CourseSelection',
        component: () => import('../views/courses/CourseSelection.vue'),
        meta: { title: '选课中心', requiresAuth: true }
      },
      {
        path: '/course-manage',
        name: 'CourseManage',
        component: () => import('../views/courses/CourseManage.vue'),
        meta: { title: '课程管理', requiresAuth: true, roles: ['teacher', 'admin'] }
      },
      {
        path: '/discussion',
        name: 'Discussion',
        component: () => import('../views/Discussion.vue'),
        meta: { title: '讨论区', requiresAuth: true }
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { title: '个人中心', requiresAuth: true }
      },
      {
        path: '/admin',
        name: 'Admin',
        component: () => import('../views/admin/AdminPanel.vue'),
        meta: { title: '管理面板', requiresAuth: true, roles: ['admin'] },
        redirect: '/admin/student',
        children: [
          {
            path: '/admin/student',
            name: 'StudentManage',
            component: () => import('../views/admin/StudentManage.vue'),
            meta: { title: '学生管理', requiresAuth: true, roles: ['admin'] }
          },
          {
            path: '/admin/teacher',
            name: 'TeacherManage',
            component: () => import('../views/admin/TeacherManage.vue'),
            meta: { title: '教师管理', requiresAuth: true, roles: ['admin'] }
          },
          {
            path: '/admin/course',
            name: 'AdminCourseManage',
            component: () => import('../views/admin/CourseManage.vue'),
            meta: { title: '课程管理', requiresAuth: true, roles: ['admin'] }
          },
          {
            path: '/admin/system',
            name: 'SystemManage',
            component: () => import('../views/admin/SystemManage.vue'),
            meta: { title: '系统设置', requiresAuth: true, roles: ['admin'] }
          }
        ]
      }
    ]
  },
  {
    path: '/unauthorized',
    name: 'Unauthorized',
    component: () => import('../views/Unauthorized.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 优化的路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('user')
  
  // 登录和注册页不需要权限检查
  if (to.path === '/login' || to.path === '/register') {
    next()
    return
  }
  
  // 未登录用户重定向到登录页
  if (!isAuthenticated && to.meta.requiresAuth) {
    next('/login')
    return
  }
  
  // 检查角色权限
  if (to.meta.roles && isAuthenticated) {
    const userRole = JSON.parse(localStorage.getItem('user'))?.role || ''
    if (to.meta.roles.includes(userRole)) {
      next()
    } else {
      // 没有权限则跳转到未授权页面
      next('/unauthorized')
    }
  } else {
    next()
  }
})

export default router 